//
//  HMStatusViewModel.swift
//  weibo10
//
//  Created by mac on 15/10/21.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class HMStatusViewModel: CustomStringConvertible{
    /// 微博模型
    var status: HMStatus
    lazy var rowHeight: CGFloat = {
        var cell: HMStatusCell
        if self.status.retweeted_status != nil {
            cell = HMRetweetsdStatusCell(style: UITableViewCellStyle.Default, reuseIdentifier: statusRetweetedReuseId)
        }else {
            cell = HMStatusNormalCell(style: UITableViewCellStyle.Default, reuseIdentifier: statusNormalReuseId)
        }
        
        return cell.rowHeight(self)
    }()
    
    /// 可重用标示符
    var cellId: String {
        return status.retweeted_status != nil ? statusRetweetedReuseId : statusNormalReuseId
    }
    
    /// 转发微博的属性
    var retweetedText: String? {
        guard let s = status.retweeted_status else {
            return nil
        }
        return "@" + (s.user?.screen_name ?? "") + ":" + (s.text ?? "")
    }
    
    /// 用户昵称
    var userScreenName: String? {
        return status.user?.screen_name
    }
    
    /// 用户头像地址
    var userProfileImageUrl: NSURL? {
        return NSURL(string: status.user?.profile_image_url ?? "")!
    }
    
    /// 默认头像
    var userProfileDefaultImage:UIImage? {
        return UIImage(named: "avatar_default_big")!
    }
    
    /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    var userVerifiedType: UIImage?{
        let type = status.user?.verified_type ?? -1
        switch(type){
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
    }
    
    /// 会员等级 0-6
    var userMbrank: UIImage?{
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    /// 缩略图数组 - URL数组
    var thumbnailUrls: [NSURL]?
    
    /// 构造函数
    init(status:HMStatus){
        self.status = status
        let urls = status.retweeted_status?.pic_urls ?? status.pic_urls
        if urls?.count > 0 {
            // 实例化缩略图数组
            thumbnailUrls = [NSURL]()
            for dict in urls! {
                let url = NSURL(string: dict["thumbnail_pic"]!)
                thumbnailUrls?.append(url!)
            }
            
        }
    }
    var description: String {
        return status.description + "配图数组 \(thumbnailUrls ?? [] as NSArray)"
    }
}